home *** CD-ROM | disk | FTP | other *** search
/ Freelog 125 / Freelog_MarsAvril2015_No125.iso / Musique / Quod Libet / quodlibet-3.3.0-portable.exe / quodlibet-3.3.0-portable / data / bin / _threading_local.pyc (.txt) < prev    next >
Python Compiled Bytecode  |  2014-12-31  |  6KB  |  243 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.7)
  3.  
  4. """Thread-local objects.
  5.  
  6. (Note that this module provides a Python version of the threading.local
  7.  class.  Depending on the version of Python you're using, there may be a
  8.  faster one available.  You should always import the `local` class from
  9.  `threading`.)
  10.  
  11. Thread-local objects support the management of thread-local data.
  12. If you have data that you want to be local to a thread, simply create
  13. a thread-local object and use its attributes:
  14.  
  15.   >>> mydata = local()
  16.   >>> mydata.number = 42
  17.   >>> mydata.number
  18.   42
  19.  
  20. You can also access the local-object's dictionary:
  21.  
  22.   >>> mydata.__dict__
  23.   {'number': 42}
  24.   >>> mydata.__dict__.setdefault('widgets', [])
  25.   []
  26.   >>> mydata.widgets
  27.   []
  28.  
  29. What's important about thread-local objects is that their data are
  30. local to a thread. If we access the data in a different thread:
  31.  
  32.   >>> log = []
  33.   >>> def f():
  34.   ...     items = mydata.__dict__.items()
  35.   ...     items.sort()
  36.   ...     log.append(items)
  37.   ...     mydata.number = 11
  38.   ...     log.append(mydata.number)
  39.  
  40.   >>> import threading
  41.   >>> thread = threading.Thread(target=f)
  42.   >>> thread.start()
  43.   >>> thread.join()
  44.   >>> log
  45.   [[], 11]
  46.  
  47. we get different data.  Furthermore, changes made in the other thread
  48. don't affect data seen in this thread:
  49.  
  50.   >>> mydata.number
  51.   42
  52.  
  53. Of course, values you get from a local object, including a __dict__
  54. attribute, are for whatever thread was current at the time the
  55. attribute was read.  For that reason, you generally don't want to save
  56. these values across threads, as they apply only to the thread they
  57. came from.
  58.  
  59. You can create custom local objects by subclassing the local class:
  60.  
  61.   >>> class MyLocal(local):
  62.   ...     number = 2
  63.   ...     initialized = False
  64.   ...     def __init__(self, **kw):
  65.   ...         if self.initialized:
  66.   ...             raise SystemError('__init__ called too many times')
  67.   ...         self.initialized = True
  68.   ...         self.__dict__.update(kw)
  69.   ...     def squared(self):
  70.   ...         return self.number ** 2
  71.  
  72. This can be useful to support default values, methods and
  73. initialization.  Note that if you define an __init__ method, it will be
  74. called each time the local object is used in a separate thread.  This
  75. is necessary to initialize each thread's dictionary.
  76.  
  77. Now if we create a local object:
  78.  
  79.   >>> mydata = MyLocal(color='red')
  80.  
  81. Now we have a default number:
  82.  
  83.   >>> mydata.number
  84.   2
  85.  
  86. an initial color:
  87.  
  88.   >>> mydata.color
  89.   'red'
  90.   >>> del mydata.color
  91.  
  92. And a method that operates on the data:
  93.  
  94.   >>> mydata.squared()
  95.   4
  96.  
  97. As before, we can access the data in a separate thread:
  98.  
  99.   >>> log = []
  100.   >>> thread = threading.Thread(target=f)
  101.   >>> thread.start()
  102.   >>> thread.join()
  103.   >>> log
  104.   [[('color', 'red'), ('initialized', True)], 11]
  105.  
  106. without affecting this thread's data:
  107.  
  108.   >>> mydata.number
  109.   2
  110.   >>> mydata.color
  111.   Traceback (most recent call last):
  112.   ...
  113.   AttributeError: 'MyLocal' object has no attribute 'color'
  114.  
  115. Note that subclasses can define slots, but they are not thread
  116. local. They are shared across threads:
  117.  
  118.   >>> class MyLocal(local):
  119.   ...     __slots__ = 'number'
  120.  
  121.   >>> mydata = MyLocal()
  122.   >>> mydata.number = 42
  123.   >>> mydata.color = 'red'
  124.  
  125. So, the separate thread:
  126.  
  127.   >>> thread = threading.Thread(target=f)
  128.   >>> thread.start()
  129.   >>> thread.join()
  130.  
  131. affects what we see:
  132.  
  133.   >>> mydata.number
  134.   11
  135.  
  136. >>> del mydata
  137. """
  138. __all__ = [
  139.     'local']
  140.  
  141. class _localbase(object):
  142.     __slots__ = ('_local__key', '_local__args', '_local__lock')
  143.     
  144.     def __new__(cls, *args, **kw):
  145.         self = object.__new__(cls)
  146.         key = ('_local__key', 'thread.local.' + str(id(self)))
  147.         object.__setattr__(self, '_local__key', key)
  148.         object.__setattr__(self, '_local__args', (args, kw))
  149.         object.__setattr__(self, '_local__lock', RLock())
  150.         if (args or kw) and cls.__init__ is object.__init__:
  151.             raise TypeError('Initialization arguments are not supported')
  152.         dict = object.__getattribute__(self, '__dict__')
  153.         current_thread().__dict__[key] = dict
  154.         return self
  155.  
  156.  
  157.  
  158. def _patch(self):
  159.     key = object.__getattribute__(self, '_local__key')
  160.     d = current_thread().__dict__.get(key)
  161.     if d is None:
  162.         d = { }
  163.         current_thread().__dict__[key] = d
  164.         object.__setattr__(self, '__dict__', d)
  165.         cls = type(self)
  166.         if cls.__init__ is not object.__init__:
  167.             (args, kw) = object.__getattribute__(self, '_local__args')
  168.             cls.__init__(self, *args, **kw)
  169.         
  170.     else:
  171.         object.__setattr__(self, '__dict__', d)
  172.  
  173.  
  174. class local(_localbase):
  175.     
  176.     def __getattribute__(self, name):
  177.         lock = object.__getattribute__(self, '_local__lock')
  178.         lock.acquire()
  179.         
  180.         try:
  181.             _patch(self)
  182.             return object.__getattribute__(self, name)
  183.         finally:
  184.             lock.release()
  185.  
  186.  
  187.     
  188.     def __setattr__(self, name, value):
  189.         if name == '__dict__':
  190.             raise AttributeError("%r object attribute '__dict__' is read-only" % self.__class__.__name__)
  191.         lock = object.__getattribute__(self, '_local__lock')
  192.         lock.acquire()
  193.         
  194.         try:
  195.             _patch(self)
  196.             return object.__setattr__(self, name, value)
  197.         finally:
  198.             lock.release()
  199.  
  200.  
  201.     
  202.     def __delattr__(self, name):
  203.         if name == '__dict__':
  204.             raise AttributeError("%r object attribute '__dict__' is read-only" % self.__class__.__name__)
  205.         lock = object.__getattribute__(self, '_local__lock')
  206.         lock.acquire()
  207.         
  208.         try:
  209.             _patch(self)
  210.             return object.__delattr__(self, name)
  211.         finally:
  212.             lock.release()
  213.  
  214.  
  215.     
  216.     def __del__(self):
  217.         import threading as threading
  218.         key = object.__getattribute__(self, '_local__key')
  219.         
  220.         try:
  221.             threads = threading._enumerate()
  222.         except:
  223.             return None
  224.  
  225.         for thread in threads:
  226.             
  227.             try:
  228.                 __dict__ = thread.__dict__
  229.             except AttributeError:
  230.                 continue
  231.  
  232.             if key in __dict__:
  233.                 
  234.                 try:
  235.                     del __dict__[key]
  236.                 except KeyError:
  237.                     pass
  238.                 
  239.  
  240.  
  241.  
  242. from threading import current_thread, RLock
  243.